#ifndef CLIENT_MANAGER_H_
#define CLIENT_MANAGER_H_

#include <ev.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include "mem_pool.h"


#define BUFFER_SIZE 4096

typedef struct {
	int type;
	int port;
	int accept_sd;
	void *accept_cb;
	pthread_rwlock_t client_list_lock;
	lpmemnode_t client_list;
} server_params_t;

typedef struct {
	struct ev_io watcher;
	uint32_t ip;
	server_params_t *params;
} client_t;

typedef struct {
	struct ev_io watcher;
	server_params_t *params;
} acceptor_t;

extern void client_manager_init();
extern void client_manager_release();
extern void all_clients_return(server_params_t *params);

extern client_t * client_alloc(server_params_t *params);
extern void client_return(client_t *data);

extern char * packet_buffer_alloc();
extern void packet_buffer_return(char *data);

extern int packet_broadcast(struct ev_loop *loop, client_t *src_cli, char *buf, ssize_t buf_len);

static inline char * ip_to_str_ul(uint32_t uip, char *sip) {
	uint8_t *ip;
	ip = (uint8_t *) &uip;
	sprintf(sip, "%d.%d.%d.%d", ip[3], ip[2], ip[1], ip[0]);
	return sip;
}

#endif // CLIENT_MANAGER_H_